﻿using System;
using System.Web.UI.HtmlControls;
using Nano.Core.Config;

namespace Nano
{
    public partial class Default : Nano.Core.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigMessage.Visible = !IsPostBack && !ConfigEngine.Instance.IsConfigured;

            if (!string.IsNullOrEmpty(Core.Web.QueryString.GetNamedItem("id")))
            {
                int pageId = int.Parse(Core.Web.QueryString.GetNamedItem("id"));
                GetPage(pageId);
                return;
            }

            LoadHomepage();
        }

        private void LoadHomepage()
        {
            var page = Core.Page.GetHomepage();

            if (page == null)
                throw new Exception("Unable to locate a valid homepage.");

            PlaceHolder1.Controls.Add(new HtmlGenericControl
            {
                InnerHtml = page.PageContent
            });

            //  Output page title.
            this.Title = ConfigEngine.Instance.WebSiteTitle + " | " + page.Title;
        }

        private void GetPage(int pageId)
        {
            var page = Core.Page.GetPage(pageId);

            if (page == null)
                throw new Exception("Unable to locate page by id.");

            PlaceHolder1.Controls.Add(new HtmlGenericControl
            {
                InnerHtml = page.PageContent
            });

            //  Output page title.
            this.Title = ConfigEngine.Instance.WebSiteTitle + " | " + page.Title;
        }
    }
}